const puppeteer = require("puppeteer");
const utils     = require('../util/utils');
const config    = require('../config/config');

class keepa{
    constructor(options) {

        if(!keepa.single){
            this.windowOptions = [
                '--disable-web-security',
                `--disable-extensions-except=${options.pluginPath},${options.wyfyPluginPath},${options.googlePluginPath}`,
                `--load-extension=${options.pluginPath},${options.wyfyPluginPath},${options.googlePluginPath}`,
                // `--proxy-server=http://${config.PROXY.HOST}:${config.PROXY.PORT}`,
                `–default-browser-check` //不检查默认浏览器
            ];
            this.browser = null;
            this.page = null;
            this.token = options.token;
            this.timeOptions = { waitUntil: ["domcontentloaded"] };
            this.closeFn = options.closeFn;
            this.username = options.username;
            this.expireTime = options.expireTime;
            this.productId = options.productId;
            this.clientToken = options.clientToken;
            this.bookmarkPath = options.bookmarkPath;
            this.decode = options.decode;
            this.monitorTimers = [];
            keepa.single = this;
        }
        return keepa.single;
    }

    async loadWindow(){
        if(this.browser){
            await this.browser.newPage();
            return;
        }
        this.browser = await puppeteer.launch({
            headless: false,
            ignoreHTTPSErrors: false,
            credentialsEnableService:false,
            ignoreDefaultArgs: ['--enable-automation','--disable-extensions'],
            defaultViewport: {
                width: 0,
                height: 0
            },
            // userDataDir: this.bookmarkPath,
            args: this.windowOptions,

        });
        this.page = await this.browser.newPage();
        // this.page.setRequestInterception(true);
        // await this.page.goto('chrome-extension://bckjlihkmgolmgkchbpiponapgjenaoa/login.html');
        await this.page.goto('https://keepa.com/#!');

    }
}
keepa.single = null;
module.exports = keepa;
